function [lpostd,likel,lpriord,lpriord_ss,flag_ok]=...
modePosterior(parest,parStru,posStru,model,dataStru,prpar,prss,filterStru,flags)
% ======================================================================
% [lpostd,likel,lpriord,lpriord_ss,flag_ok]=...
% lmjPosteriorGeneral(parest,paramVec,posStru,model,dataStru,prpar,prss,filterStru,flags)
%
%% modePosterior.m
%
% This code computes the posterior of a state space model with potentially 
% 1) NaNs in the observables 
% 2) NB Breaks, where the solution matrices have NB pages 
% 
% *No temporal aggregation* allowed  
% 
%% Inputs 
% parest:       (portion of) parameter vector being estimated
%
% parStru:      structure with
%               .param if the model is solved using a vector of parameters
%           OR  .namesEstimated if the model is solved using structures 
%
% posStru:      .param.est    rows of paramter vector estimated
%               .steady.est   rows of ss vector being estimated 
%                                   leave empty if none 
%
% model:        .handle      Needed to solve model 
%               .addsol
%               .solveOpt
%
% dataStru:     .data     [T n] vector of data
%               .trainVec [2 1] position to start and end the sample in
%                         computing the likelihood
%
% filterStru:   .tauVec   vector of indicators of which pages to use for
%                         each observation
%               .aZero    Initial state,  if flags.initialCond==1
%               .pZero    Initial VCV,    if flags.initialCond==1
%
% flags        .initialCond == 1 if the initialization of the filter is
%               given 
% 
%% Notes:  
%% Synch:  
%This function  is synched with likelNanSplitsGeneral.m 
%% Related: 
% Use modePosteriorOpt.m for optimization 
% A. Justiniano Feb 20 2014 
% =======================================================================
lpostd=-1e15;
likel=-inf;
lpriord=-inf;
lpriord_ss=-inf; 
flag_ok=0;
%% Keep this always to ensure working with latest Value
paramVec=parStru.param; 
paramVec(posStru.param.est)=parest;
% function [likel,ssvec,flag_ok,lht]=...
% likelNSplitsGeneral(parest,parStru,posStru,model,dataStru,filterStru,flags)
[likel,ssvec,flagLikelOK]=feval(filterStru.funcLikel,parest,parStru,...
    posStru,model,dataStru,filterStru,flags);
%% Likelihood
if flagLikelOK~=1; return; end
if isinf(abs(likel))==true; return; end;
if isnan(likel)==true; return; end;
%% Prior Density of Parameters
%% Modify this when ready to move to a structure base 
lpriord=sum(feval(@priordens,paramVec,prpar.prior,prpar.alphap,prpar.betap,prpar.lbnd,prpar.ubnd));
if isinf(abs(lpriord))==1;return;end;
if isnan(lpriord)==1; return;end;
%% Prior Density on Steady State
if ~isempty(posStru.steady.est)
    lpriord_ss = sum(feval(@priordens,ssvec(posStru.steady.est), prss.prior, prss.alphap, prss.betap, prss.lbnd, prss.ubnd));
    if isinf(abs(lpriord_ss))==1;return;end
else
    lpriord_ss=0;
end
%% log likelihood
lpostd = lpriord+lpriord_ss+likel;
flag_ok=1;